package cn.donghuapian.consumer;

import com.alibaba.fastjson.JSON;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.MessageProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Slf4j
@Component
public class Consumer {

    @RabbitListener(queuesToDeclare = @Queue("厄斐琉斯"))
    public void 厄斐琉斯(String msg, Channel channel, Message message) {
        System.err.println("hello" + message);
        msgAck(msg, channel, message);
    }

    @RabbitListener(queuesToDeclare = @Queue("凤凰"))
    public void hello2(String message) {
        System.err.println("hello2" + message);
    }

    @RabbitListener(bindings = {
            @QueueBinding(value = @Queue("老鼠"), // 临时队列
                    exchange = @Exchange(value = "testFanout", type = ExchangeTypes.FANOUT),
                    key = {"cc"})
    })
    public void 老鼠Fanout(String msg, Channel channel, Message message) {
        System.err.println("老鼠" + msg);
        msgAck(msg, channel, message);
    }

    @RabbitListener(bindings = {
            @QueueBinding(value = @Queue("青钢影"), // 临时队列
                    exchange = @Exchange(value = "testFanout", type = ExchangeTypes.FANOUT),
                    key = {"bb", "aa"})
    })

    public void 青钢影Fanout(String msg, Channel channel, Message message) {
        System.err.println("青钢影" + msg);
        msgAck(msg, channel, message);
    }

    @RabbitListener(queuesToDeclare = @Queue("乌鸦"))
    public void 乌鸦(String msg, Channel channel, Message message) {
        System.err.println("fine" + msg);
        msgAck(msg, channel, message);
    }


    @RabbitListener(bindings = {
            @QueueBinding(value = @Queue("exchangeDirect"), // 临时队列
                    exchange = @Exchange(value = "exchangeDirect", type = ExchangeTypes.DIRECT),
                    key = {"bb", "aa"})
    })
    public void exchangeDirect(String msg, Channel channel, Message message) {
        System.err.println("exchangeDirect" + msg);
        msgAck(msg, channel, message);
    }

    @RabbitListener(bindings = {
            @QueueBinding(value = @Queue("exchangeDirect"), // 临时队列
                    exchange = @Exchange(value = "exchangeDirect", type = ExchangeTypes.DIRECT),
                    key = {"cc", "dd"})
    })
    public void exchangeDirectCC(String msg, Channel channel, Message message) {
        System.err.println("exchangeDirectCC" + msg);
        msgAck(msg, channel, message);
    }

    //消息统一ACK
    private void msgAck(String msg, Channel channel, Message message) {
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            log.info("消息处理失败：{}", msg);
            //确认消息
            try {
                channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
                //channel对象重新发送消息到队尾
                channel.basicPublish(message.getMessageProperties().getReceivedExchange(),
                        message.getMessageProperties().getReceivedRoutingKey(), MessageProperties.PERSISTENT_TEXT_PLAIN,
                        JSON.toJSONBytes(msg));
                log.info("消息放到尾部：{}", msg);
            } catch (IOException ioException) {
                log.info("消息放到尾部失败：{}", msg);
            }
        }
    }
}
